//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Windows.Forms;

//using Microsoft.Practices.CompositeUI;
//using Microsoft.Practices.CompositeUI.SmartParts;

using GuidanceExplorer.Model;
using GuidanceExplorerAddIn.Controls;

namespace GuidanceExplorer.Controls
{
    /// <summary>
    /// Specifies the nodes to be shown in the LibrariesBrowser
    /// </summary>
    //public enum GuidanceNodeTypes
    //{
    //    /// <summary>
    //    /// All nodes
    //    /// </summary>
    //    All,
    //    /// <summary>
    //    /// Only Library nodes
    //    /// </summary>
    //    OnlyLibraries,
    //    /// <summary>
    //    /// Only Read/Write libraries
    //    /// </summary>
    //    ReadWriteLibraries,
    //    /// <summary>
    //    /// Only Read-Only libraries
    //    /// </summary>
    //    ReadOnlyLibraries,
    //    /// <summary>
    //    /// Nodes where a SearchView can be added
    //    /// </summary>
    //    SearchViewLocation,
    //    /// <summary>
    //    /// Read Write views (to drag items)
    //    /// </summary>
    //    ReadWriteViews
    //}

    public partial class LibrariesBrowser : Form
    {
        /// <summary>
        /// Current Guidance object
        /// </summary>
        private Guidance _guidance;

        /// <summary>
        /// The collection of libraries to display
        /// </summary>
        private LibraryCollection _libraries;

        /// <summary>
        /// The guidance nodes that will be displayed.
        /// </summary>
        private GuidanceNodeTypes _nodeTypes;

        /// <summary>
        /// A Form that shows the current view tree structure
        /// </summary>
        /// <param name="nodeType">Nodes to be shown</param>
        public LibrariesBrowser()
        {
            InitializeComponent();
            treeView.AfterSelect += new TreeViewEventHandler(TreeView_AfterSelect);
            //treeView.Enabled = false;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if ((_nodeTypes == GuidanceNodeTypes.SearchViewLocation ||
                _nodeTypes == GuidanceNodeTypes.ReadWriteViews) )
            {

              //  MyViewsNode myViews = MyViewsNode.New();
                LibrariesNode libsNode = LibrariesNode.New();

                if (_guidance != null)
                {
                    

                  //  treeView.Nodes.Add(myViews);

                    //if (_guidance.UserViews != null && _guidance.UserViews.Folders != null)
                    //{
                    //    AddNodes(_guidance.UserViews.Folders, _guidance.UserViews.Views, myViews);
                    //}
                }

                treeView.Nodes.Add(libsNode);
            }

            LibraryCollection libraries = null;
            if (_guidance != null)
            {
                libraries = _guidance.Libraries;
            }
            else if (_libraries != null)
            {
                libraries = _libraries;
            }

            foreach (Library library in libraries.Values)
            {
                if ((_nodeTypes == GuidanceNodeTypes.All) ||
                    (_nodeTypes == GuidanceNodeTypes.ReadOnlyLibraries && library.ReadProtection == ReadProtection.ReadOnly) ||
                    (_nodeTypes == GuidanceNodeTypes.ReadWriteLibraries && library.ReadProtection == ReadProtection.ReadWrite))
                {
                    LibraryNode node = LibraryNode.New();
                    node.ModelInstance = library;
                    node.Text = library.Caption;

                    treeView.Nodes.Add(node);
                }
                else if (_nodeTypes == GuidanceNodeTypes.SearchViewLocation ||
                    _nodeTypes == GuidanceNodeTypes.ReadWriteViews)
                {
                    if (library.ReadProtection == ReadProtection.ReadWrite)
                    {
                        LibraryNode node = LibraryNode.New();
                        node.ModelInstance = library;
                        node.Text = library.Caption;

                        treeView.Nodes[treeView.Nodes.Count - 1].Nodes.Add(node);

                        AddNodes(library.Folders, library.Views, node);
                    }
                }
            }

            treeView.ExpandAll();
        }

        void TreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (_nodeTypes == GuidanceNodeTypes.ReadWriteViews)
            {
                btnOk.Enabled = e.Node is ViewNode;
            }
            else
            {
                if (e.Node is LibrariesNode)
                {
                    btnOk.Enabled = false;
                }
                else
                {
                    btnOk.Enabled = true;
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void AddFolder(Folder folder, TreeNode parent)
        {
            FolderNode node = FolderNode.New();
            node.Text = folder.Caption;
            node.ModelInstance = folder;

            parent.Nodes.Add(node);

            AddNodes(folder.Folders, folder.Views, node);
        }

        private void AddNodes(FolderCollection folders, ViewCollection views, TreeNode node)
        {
            foreach (Folder childFolder in folders.Values)
            {
                AddFolder(childFolder, node);
            }

            if (_nodeTypes == GuidanceNodeTypes.ReadWriteViews)
            {
                foreach (GuidanceExplorer.Model.View childView in views.Values)
                {
                    if (!(childView is SearchView))
                    {
                        ViewNode viewNode = ViewNode.New();
                        viewNode.Text = childView.Caption;
                        viewNode.ModelInstance = childView;

                        node.Nodes.Add(viewNode);
                    }
                }
            }
        }

        /// <summary>
        /// Current Guidance object
        /// </summary>
        public Guidance Guidance
        {
            set
            {
                _guidance = value;
                _libraries = null;
            }
        }

        /// <summary>
        /// The collection of libraries to display
        /// </summary>
        public LibraryCollection Libraries
        {
            set
            {
                _libraries = value;
                _guidance = null;
            }
        }

        /// <summary>
        /// The guidance nodes that will be displayed.
        /// </summary>
        public GuidanceNodeTypes NodeTypes
        {
            get { return _nodeTypes; }
            set { _nodeTypes = value; }
        }

        public TreeNode SelectedNode
        {
            get { return treeView.SelectedNode; }
        }

        public GuidanceExplorer.Model.View SelectedView
        {
            get
            {
                ViewNode node = SelectedNode as ViewNode;

                if (node != null)
                {
                    return node.ModelInstance;
                }
                else
                {
                    return null;
                }
            }
        }

        public Library SelectedLibrary
        {
            get
            {
                TreeNode treeNode = treeView.SelectedNode;
                IModelItem modelNode = treeNode as IModelItem;
                if (modelNode != null)
                {
                    LibraryNode libraryNode = modelNode as LibraryNode;
                    ViewNode viewNode = modelNode as ViewNode;
                    FolderNode folderNode = modelNode as FolderNode;
                    GuidanceTypeNode guidanceTypeNode = modelNode as GuidanceTypeNode;
                    if (viewNode != null)
                    {
                        return viewNode.ModelInstance.GetContainerLibrary();
                    }
                    else if (libraryNode != null)
                    {
                        return libraryNode.ModelInstance;
                    }
                    else if (folderNode != null)
                    {
                        return folderNode.ModelInstance.GetContainerLibrary();
                    }
                    else if (guidanceTypeNode != null)
                    {
                        return null;
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
                else
                {
                    return null;
                }
            }
        }
    }
}
